package com.qqtech.qquan.homemaking.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.enums.HomemakingPubitemStatusDictEnum;
import com.qqtech.qquan.homemaking.enums.HomemakingTypeStatusDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingPubitem;
import com.qqtech.qquan.homemaking.model.HomemakingPubitemQuery;
import com.qqtech.qquan.homemaking.model.HomemakingPubitemVo;
import com.qqtech.qquan.homemaking.model.HomemakingType;
import com.qqtech.qquan.homemaking.model.HomemakingTypeQuery;
import com.qqtech.qquan.homemaking.model.HomemakingTypeVo;
import com.qqtech.qquan.homemaking.service.HomemakingPubitemService;
import com.qqtech.qquan.homemaking.service.HomemakingTypeService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/homemaking/pubitem")
public class HomemakingPubitemController extends BaseAdminController {
	@Resource
	private HomemakingPubitemService homemakingPubitemService;
	
	@Resource
	private HomemakingTypeService homemakingTypeService;

	@Override
	protected BaseService<HomemakingPubitem> getBaseService() {
		return homemakingPubitemService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<HomemakingTypeVo> parentList = homemakingTypeService
				.findParent(null);
		model.addAttribute("parentList", parentList);
		return "/homemaking/homemakingpubitem-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(HomemakingPubitemQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<HomemakingPubitemVo> page = homemakingPubitemService.queryPageListAdmin(
				query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Integer id, Model model) {
		HomemakingPubitemVo homemakingPubitemVo = homemakingPubitemService.getById(id);
		if (homemakingPubitemVo != null && homemakingPubitemVo.getTypeId() != null) {
			HomemakingTypeVo typeVo = homemakingTypeService.getById(homemakingPubitemVo.getTypeId());
			homemakingPubitemVo.setTypeName(typeVo.getName());
		}
		model.addAttribute("homemakingPubitemVo", homemakingPubitemVo);
		return "/homemaking/homemakingpubitem-edit";
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result doEdit(HomemakingPubitemVo homemakingPubitemVo) {
		Result ret = new Result();
		if (homemakingPubitemVo == null || homemakingPubitemVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(homemakingPubitemVo.getName())) {
				ret.addErr("名称不能为空");
			}
			if (StringUtil.isBlank(homemakingPubitemVo.getUnit())) {
				ret.addErr("单位不能为空");
			}
			if (homemakingPubitemVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
			if (homemakingPubitemVo.getQuantity() != null && homemakingPubitemVo.getQuantity().intValue() < 0) {
				ret.addErr("数量不能是负数");
			}
			if (homemakingPubitemVo.getPrice() != null && homemakingPubitemVo.getPrice().compareTo(BigDecimal.ZERO) == -1) {
				ret.addErr("价格不能是负数");
			}
		}
		if (ret.checkForm()) {
			HomemakingPubitem update = new HomemakingPubitem();
			update.setId(homemakingPubitemVo.getId());
			update.setName(homemakingPubitemVo.getName().trim());
			update.setStatus(homemakingPubitemVo.getStatus());
			update.setUnit(homemakingPubitemVo.getUnit().trim());
			if (homemakingPubitemVo.getQuantity() == null) {
				update.setQuantity(0);
			} else {
				update.setQuantity(homemakingPubitemVo.getQuantity());
			}
			if (homemakingPubitemVo.getPrice() == null) {
				update.setPrice(BigDecimal.ZERO);
			} else {
				update.setPrice(homemakingPubitemVo.getPrice());
			}
			homemakingPubitemService.updateById(update);
		}
		return ret;
	}

	/**
	 * 获取服务项目
	 * @param typeId 类别id
	 * @return
	 */
	@RequestMapping(value = "/findByTypeId")
	@ResponseBody
	public Result findByTypeId(@RequestBody HomemakingPubitemQuery homemakingPubitemQuery) {
		Result result = new Result();
		List<HomemakingPubitemVo> list = homemakingPubitemService.findByTypeId(
				homemakingPubitemQuery.getTypeId(), HomemakingPubitemStatusDictEnum.VALID.getCode());
		result.put("pubitemList", list);
		return result;
	}
}
